$(function () {
	
	
	$('#star').datagrid({
		
		url : "http://localhost:8080/Watson/spark.do",
		width : 1000,
		title : "Star Sparking",
		
		columns : [[
		  {
		   field : "name",
		   width : 50,
		   title : "Spark Name",
		   align : "center",
		  },
		  {
			   field : "id",
			   title : "Spark List",
			   width : 100,
			   align : "center",
			   formatter: function(value,row,index){
			  return "<a href='show.jsp?name="+row.name+"' '>《燃烧星空系列之主题为'"+row.name+"'的日志》</a>";  	}
	      },
		  {
			   field : "top",
			   align : "center",
			   title : "Top Spark Time",
		  },
		  {
			   field : "topping",
			   title : "Spark Content",
			   width : 20,
			   align : "center",
			   formatter: function(value,row,index){
			  
			  
			  return "<input type='button' value='删除' onclick='destroyUser();'></input>";  	}
	      },
		            
		            
		]],
		
		pagination : "true",
		fitColumns : "true",
		pageSize : 10,
		pageList : [5,10, 20],
		toolbar : "#sparktoolbar",
		rownumbers : "true",
		singleSelect : "true",
			
			
		
		
		
		
	});
});
	
	   

	
	
	
		  
		  
    function destroyUser(){
	var row = $('#star').datagrid('getSelected');//json ��ʽ {"teamname":"England","teamnumber":"1"} 
	                                            //����ͨ��row.teamname �õ�ֵEngland��ͨ��row.teamnumber���1 


	
	if (row){
		$.messager.confirm('Confirm','Are you sure you want to destroy this log?',function(r){
			var tiny = row.name;
			alert(tiny);
			if(r){

				$.ajax({  
                type: "GET",  
                url: "http://localhost:8080/Watson/destroy.do?name="+tiny,  
                cache: false,  
                dataType : "text",  
                success: function(data){  
				 if(data == 1){
					 $.messager.show({	
							title: 'Destory Success',
							msg: "Congratulations!"
						});
					 $('#star').datagrid('reload');
					 
					 
				 }
                }  
                
                 });   
				
                         }
			
		});
	}
}


function newUser(){
	$('#dlg').dialog('open').dialog('setTitle','New User');
	$('#fm').form('clear');
	
	url = 'http://localhost:8080/Myeclips_Pagesplit/add.do';
}


function saveUser(){
	$('#fm').form('submit',{
		url: url,
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
			//alert(result);
			if (result == 1){
				
				$('#dlg').dialog('close');  
				$.messager.show({
				title: 'Success',
				msg: "Congratulations!"
				});
			    $('#box').datagrid('reload');	
			} else{
				$.messager.show({
				title: 'Error',
				msg: "Action Failed!!"
				});
			}
		}
	});
}


function editLog(){
	var row = $('#star').datagrid('getSelected');
	//alert(row.teamname);
	name = row.name;
	alert(name);
	if (row){
		
		$.ajax({
			type : 'get',
			url : "http://localhost:8080/Watson/gain.do?name="+name,
			async : true,
			dataType : 'json',
			success : function(data) {
			//alert(data);
			//alert((JSON.stringify(data)));
			//data = JSON.stringify(data);
			//alert(data);
			var str = JSON.stringify(data);
			str = eval("("+str+")");
			var content = str.content;
			var name = str.name;
			location.href = "edit.jsp?name="+name+"&content="+content;
			
			$("#pain").val(str.name);
			CKEDITOR.instances.Content.setData(str.content);
			
		
				
					

				
			}
		});
		
		//location.href = "edit.jsp?name="+name;
		
		}
}

function searchSpark(){
	
	   
	/*$('#checkfm').form('submit',{
		url : 'http://localhost:8080/Myeclips_Pagesplit/show.do',
		url : null,
       
		onSubmit: function(){
		
		$('#box').datagrid('loadData',{ total: 0, rows: [] });
		return $(this).form('validate');
		},
		success: function(result){
			
			if (result){
				//$('#box').datagrid('loadData',result);
				alert(result);
                
				/*$('#box').datagrid('load', { 

				searchname: $("#checkname").val(), 

				searchnumber: $("#checknumber").val() 

				}); */
				/*$('#box').datagrid({
					
				url : "http://localhost:8080/Myeclips_Pagesplit/search.do?searchname="+$("#checkname").val()+"&searchnumber="+$("#checknumber").val()+""
				});
				
				
			} else{
				$('#box').datagrid('loadData',{ total: 0, rows: [] });
				$.messager.show({
				title: 'Error',
				msg: "No record checked!!"
				});
			}
		}
	});
	*/
    /*$('#box').datagrid({
		url : "search_json.jsp?searchname="+$("#checkname").val()+"&searchnumber="+$("#checknumber").val()+"String page,String rows" 
	});
	$('#box').datagrid('reload');	*/

	
	$.ajax({
		type : 'get',
		url : "http://localhost:8080/Watson/check.do?name="+$("#checkname").val()+"&page=1&rows=10",
		async : true,
		dataType : 'json',
		success : function(data) {
		       
		        //alert(data);
		       
				$('#star').datagrid({
					
				url : "http://localhost:8080/Watson/check.do?name="+$("#checkname").val()+""
				});
			
				

			if(data == null){
				$('#star').datagrid('loadData',{ total: 0, rows: [] });
				$.messager.show({
				title: 'Error',
				msg: "No record checked!!"
				});
			}
		}
	});
	
	
	
}

function showAll(){
	$('#star').datagrid({
		url : "http://localhost:8080/Watson/spark.do",
	});
}

function addNew(){
	
	location.href = "index.jsp";
}


function showjson(){
	
	   

	
	$.ajax({
		type : 'get',
		url : 'http://localhost:8080/Myeclips_Pagesplit/show.do?page=5&rows=10',
		async : true,
		dataType : 'json',
		success : function(data) {
		alert((JSON.stringify(data)));
		}
	});
	
	
	
}